package cn.whale.kill.web.controller;

import cn.whale.basic.utils.IdWorker;
import cn.whale.kill.dto.KillParamsDto;
import cn.whale.kill.service.IKillCourseService;
import cn.whale.kill.domain.KillCourse;
import cn.whale.kill.query.KillCourseQuery;
import cn.whale.basic.utils.JSONResult;
import cn.whale.basic.utils.PageList;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.baomidou.mybatisplus.plugins.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/killCourse")
@Slf4j
public class KillCourseController {
    @Autowired
    public IKillCourseService killCourseService;

    IdWorker idWorker = IdWorker.getInstance();

    //秒杀流程
    @SentinelResource(value="coursekill",blockHandler="killExceptionHandler",defaultFallback="killFallbackHandler")
    @RequestMapping(value = "/kill",method = RequestMethod.POST)
    public JSONResult kill(@RequestBody KillParamsDto dto){
        try{
            //订单号
            dto.setOrderNo(String.valueOf(idWorker.nextId()));
            killCourseService.kill(dto);
            return JSONResult.success(dto.getOrderNo());
        }catch (Exception e){
            return JSONResult.error(e.getMessage());
        }
    }
    // 限流与阻塞处理 : 参数要和 被降级的方法参数一样
    public JSONResult killExceptionHandler(@RequestBody KillParamsDto dto, BlockException ex) {
        ex.printStackTrace();
        log.error("秒杀业务限流了{}",ex.getMessage());
        return JSONResult.error("秒杀失败,您可以重新发起秒杀请求");
    }
    // 熔断降级，参数和返回值与源方法一致
    public JSONResult killFallbackHandler(@RequestBody KillParamsDto dto){
        log.error("秒杀业务异常，触发降级");
        return JSONResult.error("秒杀失败,您可以重新发起秒杀请求");
    }
    //查询某个场次下的所有商品
    @RequestMapping(value = "/listBySessionId/{sessionId}",method = RequestMethod.GET)
    public JSONResult listBySessionId(@PathVariable("sessionId") Long sessionId){
        return JSONResult.success(killCourseService.listBySessionId(sessionId));
    }


    /**
    * 保存和修改公用的
    * @param killCourse  传递的实体
    * @return Ajaxresult转换结果
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult save(@RequestBody KillCourse killCourse){
        if(killCourse.getId()!=null){
            killCourseService.updateById(killCourse);
        }else{
            killCourseService.insert(killCourse);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        killCourseService.deleteById(id);
        return JSONResult.success();
    }

    //获取用户
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(killCourseService.selectById(id));
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(killCourseService.selectList(null));
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult json(@RequestBody KillCourseQuery query)
    {
        Page<KillCourse> page = new Page<KillCourse>(query.getPage(),query.getRows());
        page = killCourseService.selectPage(page);
        return JSONResult.success(new PageList<KillCourse>(page.getTotal(),page.getRecords()));
    }
}
